package com.example.safeassistan.risk.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.activity.HiddenDangerinvestigation;
import com.example.safeassistan.home.adapter.InvestigationTab1Adapter;
import com.example.safeassistan.home.adapter.InvestigationTabAdapter;
import com.example.safeassistan.home.fragment.InvestagionFragment;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.adapter.RiskPointTableDetailListPAdapter;
import com.example.safeassistan.risk.fragment.RiskPointTableDetailFragment;
import com.example.safeassistan.risk.model.RiskPointPersonModel;
import com.example.safeassistan.risk.model.RiskPointTableDetailModel;
import com.example.safeassistan.risk.model.RiskPointtableModel;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.utile.ToastUtil;
import com.example.safeassistan.widget.CustomExpandableListView;
import com.example.safeassistan.widget.HiddendangerInvestigationDialog;
import com.example.safeassistan.widget.WarningDialog;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RiskPointTableDetailActivity extends BaseActivity implements View.OnClickListener {
    private String riskId="",tableName="",riskpointId="";//检查表id 检查表名称 风险点id
    private InvestigationTab1Adapter tabAdapter;
    private RecyclerView rcView;
    private ViewPager viewPager;
    private TextView tvTableName;


    private List<RiskPointtableModel.ResultDTO.DangerVOListDTO> model;
    private List<Fragment> fragmentList;
    private MainAdapter mainAdapter;

    public static void actionStart(Context context,String riskId,String tableName,List<RiskPointtableModel.ResultDTO.DangerVOListDTO> model,String riskpointId) {
        Intent intent = new Intent(context, RiskPointTableDetailActivity.class);
        intent.putExtra("riskId",riskId);
        intent.putExtra("tableName",tableName);
        intent.putExtra("model",(Serializable) model);
        intent.putExtra("riskpointId",riskpointId);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.riskpoint_table_detail;
    }

    @Override
    protected void initView() {
        setTitleText("隐患排查");
        tableName=getIntent().getStringExtra("tableName");
        riskId=getIntent().getStringExtra("riskId");
        riskpointId=getIntent().getStringExtra("riskpointId");
        model=(List<RiskPointtableModel.ResultDTO.DangerVOListDTO>)getIntent().getSerializableExtra("model");
        tvTableName=findViewById(R.id.tvTableName);
        viewPager=findViewById(R.id.viewPager);
        rcView=findViewById(R.id.rcView);
        tvTableName.setText(tableName);

        if (model!=null&&model.size()>0){
            fragmentList=new ArrayList<>();

            for (int i=0;i<model.size();i++){
                fragmentList.add(RiskPointTableDetailFragment.newInstance(model.get(i).getId(),riskpointId,riskId));
            }
            mainAdapter=new MainAdapter(getSupportFragmentManager());
            viewPager.setAdapter(mainAdapter);
            tabAdapter=new InvestigationTab1Adapter(model);
            rcView.setAdapter(tabAdapter);
            rcView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
            tabAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    for (int i=0;i<model.size();i++){
                        model.get(i).setSelect(false);
                    }
                    model.get(position).setSelect(true);
                    tabAdapter.notifyDataSetChanged();
                    viewPager.setCurrentItem(position);
                }
            });

            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    for (int i=0;i<model.size();i++){
                        model.get(i).setSelect(false);
                    }
                    model.get(position).setSelect(true);
                    tabAdapter.notifyDataSetChanged();
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }




    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return model.get(position).getDangerSrcName();
        }
    }
}